package org.hyl.javabase.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class dao implements IBaseDao {
	Connection conn = null;
	private PreparedStatement stmt = null;
	String viewname;

	@Override
	public List<Map<String, Object>> select(String sql, Object[] ps) {
		// ����List
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		try {
			stmt = conn.prepareStatement(sql);
			for (int i = 0; i < ps.length; i++) {
				stmt.setObject(i + 1, ps[i]);
			}
			ResultSet rs = stmt.executeQuery();
			// ����ResultSetMetaData����
			ResultSetMetaData rsmd = null;

			// ���������
			int columnCount = 0;
			try {
				rsmd = rs.getMetaData();
				// ��ý��������
				columnCount = rsmd.getColumnCount();
			} catch (SQLException e1) {
				System.out.println(e1.getMessage());
			}

			// չ����������ݿ� ��ѭ������ÿһ��
			while (rs.next()) {
				Map<String, Object> map = new HashMap<String, Object>();
				for (int i = 1; i <= columnCount; i++) {
					map.put(rsmd.getColumnLabel(i), rs.getObject(i));
				}
				list.add(map);// ÿһ��map����һ����¼�������м�¼����list��
			}
			// ��ɺ�ر�
			rs.close();
		} catch (SQLException se) {
			// ���� JDBC ����
			se.printStackTrace();
		} catch (Exception e) {
			// ���� Class.forName ����
			e.printStackTrace();
		} finally {
			close();
		}
		return list;
	}

	@Override
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int update(HashMap<?, ?> s, String where) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(HashMap<?, ?> row) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int inserts(List<HashMap> row) {
		// TODO Auto-generated method stub
		return 0;
	}

	static final String DB_URL = "jdbc:mariadb://192.168.1.113:3306/t1";
	// ���ݿ���û��������룬��Ҫ�����Լ�������
	static final String USER = "hyl";
	static final String PASS = "hyl";

	@Override
	public boolean conn() {
		try {
			// ע�� JDBC ����
			Class.forName("org.mariadb.jdbc.Driver");
			// ������
			System.out.println("�������ݿ�...");
			conn = DriverManager.getConnection(DB_URL, USER, PASS);
			// ִ�в�ѯ
			System.out.println(" ʵ����Statement����...");
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean close() {
		// �ر���Դ
		try {
			if (stmt != null)
				stmt.close();
		} catch (SQLException se2) {
			return false;
		} // ʲô������

		try {
			if (conn != null)
				conn.close();
		} catch (SQLException se) {
			se.printStackTrace();
			return false;
		}
		return false;

	}

	@Override
	public Connection getConn() {
		return conn;

	}

	@Override
	public void setView(String viewname) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<?> select(String[] columns) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<?> select(String sql) {
		// TODO Auto-generated method stub
		return null;
	}



}
